package com.kk.filter;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.filter.DelegatingFilterProxy;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.util.EnumSet;

/**
 * @author: kevin
 * @data: 2018-08-31 15:57
 * @desc:   容器初始化时注入tokenAuthFilter类
 */
@Configuration
public class TokenAuthConfig implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy();
        delegatingFilterProxy.setTargetBeanName("tokenAuthFilter");
        delegatingFilterProxy.setTargetFilterLifecycle(true);
        FilterRegistration filterRegistration = servletContext.addFilter("tokenAuthFilter", delegatingFilterProxy);
        filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/user/*");
    }
}
